// 全局方法
const image = function (file) {
    const isJpgOrPng = file.type === 'image/jpeg' || file.type === 'image/png';

    if (!isJpgOrPng) {
        return false
        // this.$message.error('你上传的不是图片');
    } else {
        return true
    }

};

const pageNum = function(val) {
    if (val == 0) {
        return 1
    } else {
        return val
    }
}

const pdf = function (file) {
    const isPdf = file.type === 'application/pdf'
    if (!isPdf) {
        return false
        // this.$message.error('你上传的不是PDF');
    } else {
        return true
    }
}


const Base64ToBlob = function (urlData, type = 'image/jpeg') {
    try {
        var arr = urlData.split(',')
        var mime = arr[0].match(/:(.*?);/)[1] || type;
        // 去掉url的头，并转化为byte
        var bytes = window.atob(arr[1]);
        // 处理异常,将ascii码小于0的转换为大于0
        var ab = new ArrayBuffer(bytes.length);
        // 生成视图（直接针对内存）：8位无符号整数，长度1个字节
        var ia = new Uint8Array(ab);

        for (var i = 0; i < bytes.length; i++) {
            ia[i] = bytes.charCodeAt(i);
        }

        return new Blob([ab], {
            type: mime
        });
    }
    catch (e) {
        var ab = new ArrayBuffer(0);
        return new Blob([ab], {
            type: type,
        });
    }
}
// 判断浏览器是否为ie浏览器
const isIE=function() {
    const bw = window.navigator.userAgent
    const compare = (s) => bw.indexOf(s) >= 0
    const ie11 = (() => 'ActiveXObject' in window)()
    return compare('MSIE') || ie11
}
//返回后缀名
const checkFileType=function(str) {
    var strRegex = '(.pdf)$'
    var re = new RegExp(strRegex)
    if (re.test(str.toLowerCase())) {
        return 'pdf'
    } else if (new RegExp('(.mp4)$').test(str.toLowerCase())) {
        return 'mp4'
    }
    else {
        return 'img'
    }


}

// 计算字符长度
export const DataLength = (fData) => {
    let intLength = 0
    for (let i = 0; i < fData.length; i++) {
        if ((fData.charCodeAt(i) < 0) || (fData.charCodeAt(i) > 255))
            intLength = intLength + 2
        else
            intLength = intLength + 1
    }
    return intLength
}



export default { image, pdf, pageNum, Base64ToBlob,isIE,checkFileType ,DataLength}
